﻿package com.view 
{
	import adobe.utils.CustomActions;
	import com.yahoo.astra.fl.containers.TilePane;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author zheng sir
	 */
	public class ClothMenuMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "clothMenuMediator_name";
		public static const CLOTHCHANGED:String = "clothMenuMeidator_clothChanged";
		public static const HEADWEARCHANGED:String = "clothMenuMeidator_headWearChanged";
		public static const HOODCHANGED:String = "clothMenuMediator_hoodChanged";
		public static const BRACELETCHANGED:String = "clothMenuMediator_braceletChanged";
		public static const NECKLACECHANGED:String = "clothMenuMediator_necklace_changed";
		
		public function ClothMenuMediator(mediatorName:String = null, viewComponent:Object = null) 
		{
			super(mediatorName, viewComponent);
			
		}
		override public function onRegister():void 
		{
			super.onRegister();
			layout();
		}
		override public function listNotificationInterests():Array 
		{
			return super.listNotificationInterests();
		}
		override public function handleNotification(notification:INotification):void 
		{
			super.handleNotification(notification);
		}
		/**
		 * 布局
		 */
		private function layout():void {
			var tilePane:TilePane = viewComponent.clothMenuMc.tilePane as TilePane;
			tilePane.horizontalGap = 2;
			tilePane.verticalGap = 2;
			for (var i:int=1; i <= 15; i++ ) {
				var hoodIcon:MovieClip = new Element1();
				hoodIcon.showMc.gotoAndStop(i);
				tilePane.addChild(hoodIcon);
				hoodIcon.num = i;
				hoodIcon.addEventListener(MouseEvent.CLICK, onHoodClick);
				addBtnEvent(hoodIcon);
			}
			for (var j:int = 1; j <= 15; j++ ) {
				var clothIcon:MovieClip = new Element2();
				clothIcon.showMc.gotoAndStop(j);
				tilePane.addChild(clothIcon);
				clothIcon.num = j;
				clothIcon.addEventListener(MouseEvent.CLICK, onClothClick);
				addBtnEvent(clothIcon);
			}
			//手链
			for (var k:int = 1; k <= 5; k++ ) {
				var braceletIcon:MovieClip = new Element3();
				braceletIcon.showMc.gotoAndStop(k);
				tilePane.addChild(braceletIcon);
				braceletIcon.num = k;
				braceletIcon.addEventListener(MouseEvent.CLICK, onBraceletClick);
				addBtnEvent(braceletIcon);
			}
			//头饰
			for (var l:int = 1; l <= 3; l++ ) {
				var headwearIcon:MovieClip = new Element4();
				headwearIcon.showMc.gotoAndStop(l);
				tilePane.addChild(headwearIcon);
				headwearIcon.num = l;
				headwearIcon.addEventListener(MouseEvent.CLICK, onHeadwearClick);
				addBtnEvent(headwearIcon);
			}
			for (var m:int = 1; m <= 3; m++ ) {
				var necklaceIcon:MovieClip = new Element5();
				necklaceIcon.showMc.gotoAndStop(m);
				tilePane.addChild(necklaceIcon);
				necklaceIcon.num = m;
				necklaceIcon.addEventListener(MouseEvent.CLICK, onNecklaceClick);
				addBtnEvent(necklaceIcon);
			}
		}
		
		private function onNecklaceClick(e:MouseEvent):void 
		{
			var num:int = MovieClip(e.currentTarget).num;
			sendNotification(NECKLACECHANGED, num);
		}
		
		private function onBraceletClick(e:MouseEvent):void 
		{
			var num:int = MovieClip(e.currentTarget).num;
			sendNotification(BRACELETCHANGED, num);
		}
		
		private function onHoodClick(e:MouseEvent):void 
		{
			var num:int = MovieClip(e.currentTarget).num;
			sendNotification(HOODCHANGED, num);
		}
		private function onHeadwearClick(e:MouseEvent):void 
		{
			var num:int = MovieClip(e.currentTarget).num;
			sendNotification(HEADWEARCHANGED, num);
		}
		private function onClothClick(e:MouseEvent):void 
		{
			var num:int = MovieClip(e.currentTarget).num;
			sendNotification(CLOTHCHANGED, num);
		}
		private function addBtnEvent(mc:MovieClip):void {
			mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			mc.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			mc.addEventListener(MouseEvent.CLICK, onClick);
		}
		private function delBtnEvent(mc:MovieClip):void {
			mc.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
			mc.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
			mc.removeEventListener(MouseEvent.CLICK, onClick);
		}
		private function onRollOver(e:MouseEvent ):void {
			var btnMusic:MovieClip = facade.retrieveMediator(ApplicationMediator.NAME).getViewComponent().btnMusic as MovieClip;
			btnMusic.gotoAndPlay(2);
			MovieClip(e.currentTarget).scaleX = ApplicationMediator.SCALEXY;
			MovieClip(e.currentTarget).scaleY = ApplicationMediator.SCALEXY;
		}
		private function onRollOut(e:MouseEvent):void 
		{
			MovieClip(e.currentTarget).scaleX = 1;
			MovieClip(e.currentTarget).scaleY = 1;
		}
		private function onClick(e:MouseEvent):void {
			var btnMusic:MovieClip = facade.retrieveMediator(ApplicationMediator.NAME).getViewComponent().btnMusic as MovieClip;
			btnMusic.gotoAndPlay(4);
		}
	}

}